package boundarys;

import java.awt.EventQueue;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JSeparator;

import java.awt.Checkbox;

import javax.swing.JEditorPane;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import controllers.AddReportController;
import entitys.Product;
import entitys.Reply;
import entitys.Session;
import enums.Command;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JTextPane;

public class AddReportGUI extends AbstractGUI {
	/**
	 * Frame of Add Report
	 */
	private JFrame frame;
	/**
	 * ArrayList that contains all Products. 
	 */
	ArrayList<Product> arr=null;
	/**
	 * Label of picture.
	 */
	JLabel lblPiclable=null;
	/**
	 * Text pane for product's description.
	 */
	JTextPane productTextPane=null;
	/**
	 * Constructor of Add Report GUI.
	 * @param marketingSaleID 
	 */
	public AddReportGUI(int cutomerID, String marketingSaleID) {
		initialize();
		initGUI(frame);
		frame.getContentPane().setLayout(null);
		AddReportController.getProducts(marketingSaleID);
		lblPiclable = new JLabel("");
		lblPiclable.setBounds(459, 45, 214, 212);
		frame.getContentPane().add(lblPiclable);
		productTextPane = new JTextPane();
		productTextPane.setEditable(false);
		productTextPane.setBounds(58, 11, 381, 104);
		frame.getContentPane().add(productTextPane);
		JPanel notSoldPanel = new JPanel();
		notSoldPanel.setBounds(21, 126, 419, 176);
		frame.getContentPane().add(notSoldPanel);
		notSoldPanel.setLayout(null);
		
		JEditorPane dtrpnWriteYourReview = new JEditorPane();
		dtrpnWriteYourReview.setText("Review");
		dtrpnWriteYourReview.setBounds(0, 0, 419, 176);
		notSoldPanel.add(dtrpnWriteYourReview);
		frame.setVisible(true);
		JCheckBox chckbxSold = new JCheckBox("Sold?");
		chckbxSold.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if(chckbxSold.isSelected())
				{
					dtrpnWriteYourReview.enable(false);
				}
				else
				{
					dtrpnWriteYourReview.enable(true);
				}
			}
		});
		chckbxSold.setBounds(6, 11, 65, 23);
		frame.getContentPane().add(chckbxSold);
		
		JButton btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddReportController.addAddressSale((chckbxSold.isSelected()?1:0), Integer.parseInt(productTextPane.getText().substring(productTextPane.getText().indexOf("#")+1, productTextPane.getText().indexOf(" "))), cutomerID, Session.getEmployee().getId(), dtrpnWriteYourReview.getText(),marketingSaleID);
				goBack();
			}
		});
		btnConfirm.setBounds(174, 306, 89, 23);
		frame.getContentPane().add(btnConfirm);
		

		

		

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 699, 379);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	@Override
	/**
	 * AbstractGUI overridden method for getting a reply from server.
	 */
	public void getReply(Object r) {
		if(((Reply)r).command.equals(Command.GET_PRODUCTS_BY_MARKETING_SALE)) //if GET_PRODUCTS reply is received
		{
			String s =(String) ((Reply)r).result;
			if(s!=null)
			{
				BufferedImage myPicture = null;
				try {
					myPicture = ImageIO.read(new File("images\\"+s.substring(0, s.indexOf(" "))+".png"));
					
				s=s.substring(s.indexOf(" ")+1);
			
				Image myPictureScaled=myPicture.getScaledInstance(lblPiclable.getWidth(), lblPiclable.getHeight(), myPicture.SCALE_SMOOTH);
				frame.getContentPane().remove(lblPiclable);
				frame.getContentPane().validate();
				frame.getContentPane().repaint();
				lblPiclable = new JLabel(new ImageIcon(myPictureScaled));
				lblPiclable.setBounds(459, 45, 214, 212);
				frame.getContentPane().add(lblPiclable);
				frame.getContentPane().revalidate();
				frame.getContentPane().repaint();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					frame.getContentPane().remove(lblPiclable);
					frame.getContentPane().validate();
					frame.getContentPane().repaint();
					lblPiclable = new JLabel("Image is not defined.");
					lblPiclable.setBounds(459, 45, 214, 212);
					frame.getContentPane().add(lblPiclable);
					frame.getContentPane().revalidate();
					frame.getContentPane().repaint();
					System.out.println("Picture isn't defined.");
				}
				catch (java.lang.NullPointerException e1)
				{
					System.out.println("picture wasn't found.");
				}
				
				finally
				{
					productTextPane.setText(s);
				}
			}
			/*	arr =(ArrayList<Product>) ((Reply)r).result;

			
			for(int i=0;i<arr.size();i++)
			{
				productsComboBox.addItem(arr.get(i));
			}
			*/
		}
		
	}
}
